﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Data.SQLite;
using MedicLibrary.Controler;

namespace MedicLibrary.Model
{


    public class NhomThuoc
    {
        public int ID { get; set; }
        public String MaNhom { get; set; }
        public String TenNhom { get; set; }
        public int NhomChaID { get; set; }
        public String TenNhomCha { get; set; }
        public String DacDiem { get; set; }
        public String TacDungPhu { get; set; }
        public String GhiChu { get; set; }

        public NhomThuoc()
        { 
        }

        public static DataTable getDanhSachNhomThuoc( String para)
        {
            DBConnector dbc = new DBConnector();
            String sql = "";
            if (para != "")
            {
                sql = "SELECT * FROM NhomThuoc WHERE TenNhom LIKE '%" + para + "%' OR MaNhom LIKE '%" + para + "%' LIMIT 100";
            }else
                sql = "SELECT * FROM NhomThuoc LIMIT 100";
            DataTable table = dbc.GetDataTable(sql);
            table.Columns.Add("TenNhomCha");
            DataTable temp = new DataTable();
            for (int i = 0; i < table.Rows.Count; i++ )
            {
                temp = dbc.GetDataTable("SELECT TenNhom FROM NhomThuoc WHERE ID = " + table.Rows[i]["NhomChaID"]);
                if (temp.Rows.Count > 0)
                {
                    table.Rows[i]["TenNhomCha"] = temp.Rows[0]["TenNhom"];
                }else
                    table.Rows[i]["TenNhomCha"] = "Nhóm Gốc";
            }
            return table;
        }

        public static DataTable getThongTinNhomThuoc(int ID)
        {
            DBConnector dbc = new DBConnector();
            String sql = "SELECT * FROM NhomThuoc";
            sql += " WHERE ID =" + ID;
            DataTable table = dbc.GetDataTable(sql);
            table.Columns.Add("TenNhomCha");
            DataTable temp = new DataTable();
                temp = dbc.GetDataTable("SELECT TenNhom FROM NhomThuoc WHERE ID = " + table.Rows[0]["NhomChaID"]);
                if (temp.Rows.Count > 0)
                {
                    table.Rows[0]["TenNhomCha"] = temp.Rows[0]["TenNhom"];
                }
                else
                    table.Rows[0]["TenNhomCha"] = "Nhóm Gốc";
            return table;
        }

        public bool readThongTinNhomThuoc(int ID)
        {
            DataTable table = getThongTinNhomThuoc(ID);
            if (table.Rows.Count == 0)
                return false;
            foreach (DataRow r in table.Rows)
            {
                  
                this.ID = Int32.Parse(r["ID"].ToString());
                this.MaNhom = r["MaNhom"].ToString();
                this.TenNhom = r["TenNhom"].ToString();
                this.NhomChaID = Int32.Parse(r["NhomChaID"].ToString());
                this.TenNhomCha = r["TenNhomCha"].ToString();
                this.DacDiem = r["DacDiem"].ToString();
                this.TacDungPhu = r["TacDungPhu"].ToString();
                this.GhiChu = r["GhiChu"].ToString();
            }
            return true;
        }

        public bool insertNhomThuoc()
        {
            DBConnector dbc = new DBConnector();
            Dictionary<String, String> data = new Dictionary<String, String>();
            data.Add("ID", this.ID.ToString());
            data.Add("MaNhom", "NHOM" + this.ID.ToString());
            data.Add("TenNhom", this.TenNhom);
            data.Add("NhomChaID", this.NhomChaID.ToString());
            data.Add("DacDiem", this.DacDiem);
            data.Add("TacDungPhu", this.TacDungPhu);
            data.Add("GhiChu", this.GhiChu);
            try
            {
                dbc.Insert("NhomThuoc", data);
            }
            catch(Exception crap)
            {
                MessageBox.Show(crap.Message);
                return false;
            }
            MessageBox.Show("Thêm Nhóm Thuốc thành công!");
            return true;
        }

        public bool updateNhomThuoc()
        {
            DBConnector dbc = new DBConnector();
            Dictionary<String, String> data = new Dictionary<String, String>();
            data.Add("MaNhom", this.MaNhom);
            data.Add("TenNhom", this.TenNhom);
            data.Add("NhomChaID", this.NhomChaID.ToString());
            data.Add("DacDiem", this.DacDiem);
            data.Add("TacDungPhu", this.TacDungPhu);
            data.Add("GhiChu", this.GhiChu);
            try
            {
               dbc.Update("NhomThuoc", data, String.Format("NhomThuoc.ID = {0}", this.ID));
            }
            catch (Exception crap)
            {
                MessageBox.Show(crap.Message);
                return false;
            }
            MessageBox.Show("Cập nhật Nhóm Thuốc thành công!");
            return true;
        }

        public bool deleteNhomThuoc()
        {
            if (MessageBox.Show("Bạn thật sự muốn xóa Nhóm Thuốc này ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                DBConnector dbc = new DBConnector();
                return dbc.Delete("NhomThuoc", String.Format("ID = {0}", this.ID));
            }
            else
                return false;
        }

       public void getMaxID()
        { 
            DBConnector dbc = new DBConnector();
            String sql = "SELECT MAX(ID) as MAXID FROM NhomThuoc";
            this.ID = Int32.Parse(dbc.GetDataTable(sql).Rows[0]["MAXID"].ToString()) + 1;
        }

        public void getParentID()
        {
            DBConnector dbc = new DBConnector();
            String sql = "SELECT NhomChaID FROM NhomThuoc WHERE ID = " + this.ID;
            this.NhomChaID = Int32.Parse(dbc.GetDataTable(sql).Rows[0]["NhomChaID"].ToString());
        }

        public int getParentIDfromName(String name)
        {
            DBConnector dbc = new DBConnector();
            String sql = "SELECT ID FROM NhomThuoc WHERE TenNhom = '" + name +"'";
            return Int32.Parse(dbc.GetDataTable(sql).Rows[0]["ID"].ToString());
        }

    }
}
